package com.message.queue.rabbit.base;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @包名称 com.message.queue.rabbit.base
 * @类描述 <pre></pre>
 * @作者 zhangbo
 * @创建时间 2020/7/16.
 * @版本 1.00
 * @修改记录 <pre>
 *     版本           修改人         修改日期         修改内容描述
 *     -----------------------------------------------------------
 *     1.00           zhangbo        2020/7/16.           类创建
 *     -----------------------------------------------------------
 * </pre>
 */
public class Producer {

    public static void main(String[] args) throws IOException, TimeoutException {
        /*1、创建一个连接工厂*/
        ConnectionFactory connectionFactory = new ConnectionFactory();
        /*配置参数属性*/
        connectionFactory.setHost("172.16.24.182");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");

        /*设置用户名与密码*/
//        connectionFactory.setUsername("");
//        connectionFactory.setPassword("");

        /*2、创建一个连接*/
        Connection connection = connectionFactory.newConnection();

        /*3、创建一个channel*/
        String echangeName = "my_exchange";
        String routingKey = "testKey";
        Channel channel = connection.createChannel();


        /*4、通过channel发送数据*/
        for (int i = 0; i < 5; i++) {
            System.out.println("发送消息：" + i);
            String msg = "Hello RabbitMQ! " + i;
            /*需要先配置交换机与队列的绑定关系，同时必须*/
            channel.basicPublish(echangeName, routingKey, null, msg.getBytes());
        }

        /*关闭连接*/
        channel.close();
        connection.close();
    }
}
